﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Mvc;
using OpenFlashChart;
using Peak.Core;
using Peak.Core.DataInterfaces;
using Peak.Resources;
using Peak.Web.Controllers.Models;
using Peak.Web.Controllers.Services;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Web.NHibernate;
using MvcContrib;

namespace Peak.Web.Controllers
{
  public class SearchController : BaseController
  {
    private readonly ITaskRepository _taskRepository;
    private readonly IStoryRepository _storyService;

    public SearchController(ISessionService sessionService, ITaskRepository taskRepository, IStoryRepository storyService)
      : base(sessionService)
    {      
      _taskRepository = taskRepository;
      _storyService = storyService;
    }

    public ActionResult Index()
    {
      return View("Results",new SearchModel());
    }

    public ActionResult Results(string quickFilter)
    {
      SearchModel resultsModel = new SearchModel();      
      if (!string.IsNullOrEmpty(quickFilter))
      {
        resultsModel.QuickFilter = quickFilter;
       Page<Task> tasks =
          _taskRepository.GetPage(new TaskFilter() {QuickFilter = quickFilter, ProjectId = _sessionService.ProjectId});
        Page<Story> stories =
          _storyService.GetPage(new StoryFilter() {QuickFilter = quickFilter, ProjectId = _sessionService.ProjectId});
        resultsModel.AddTasks(tasks.Results);
        resultsModel.AddStory(stories.Results);
      }
      return View(resultsModel);
    }    
  }
}
